package logic

import (
	"cart_srv/models"
	"context"
	"errors"

	"cart_srv/cart"
	"cart_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateGoodsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreateGoodsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateGoodsLogic {
	return &CreateGoodsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CreateGoodsLogic) CreateGoods(in *cart.CreateGoodsRequest) (*cart.CreateGoodsResponse, error) {
	// todo: add your logic here and delete this line
	var err error
	var g models.Goods

	if in.Title == "" || in.Price == 0 || in.Stock == 0 {
		logx.Error("参数不能为空")
		return nil, errors.New("参数不能为空")
	}

	g.ShopId = in.ShopId
	g.Title = in.Title
	g.Price = float64(in.Price)
	g.Stock = in.Stock
	if err = g.CreateGoods(); err != nil {
		logx.Error("商品添加失败")
		return nil, errors.New("商品添加失败")
	}

	return &cart.CreateGoodsResponse{
		GoodsId: int64(g.ID),
	}, nil
}
